package com.moleq.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.enums.ItemType;
import com.moleq.logic.SaleList;
import com.moleq.logic.TransactionManager;
import com.moleq.mgpos.activities.R;
import com.moleq.models.FreeFuncRpt;
import com.moleq.models.GlobalInfo;
import com.moleq.models.SaleListItem;

public class ShiftInfoDialog extends MoleQBaseDialog {

	private Button btnConfirm;
	private Button btnClose;
	private CheckBox cb_tax1,cb_tax2,cb_tax3,cb_tax4,cb_tax5,cb_tax6,cb_foodstamp;
	private SaleList saleList;
	private SaleListItem item;
	private int itemIdx;
	private boolean isshifttax;
	private boolean isshiftfs;
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.buttonShiftInfoClose:
				dismiss();
				break;
			case R.id.buttonShiftInfoConfirm:
				
				item.setTax1(cb_tax1.isChecked());
				item.setTax2(cb_tax2.isChecked());
				item.setTax3(cb_tax3.isChecked());
				item.setTax4(cb_tax4.isChecked());
				item.setTax5(cb_tax5.isChecked());
				item.setTax6(cb_tax6.isChecked());
				item.setFoodstamp(cb_foodstamp.isChecked());
				
//				saleList.mSaleItems.set(itemIdx, item);
				saleList.selectedIndex(itemIdx);
				TransactionManager.getInstance().announceChanges();
				
				if (isshiftfs) {
					GlobalInfo.getInstance().useFuntionsList.add(GlobalInfo.getInstance().CurrentFreeFuncID);
					TransactionManager.getInstance().SaveFreeFuncRpt(FreeFuncRpt.SHIFT_FOODSTAMP, 1, 0);
				}
				if (isshifttax) {
					GlobalInfo.getInstance().useFuntionsList.add(GlobalInfo.getInstance().CurrentFreeFuncID);
					TransactionManager.getInstance().SaveFreeFuncRpt(FreeFuncRpt.SHIFT_TAX, 1, 0);
				}
				onCloseWithReturn(arg0.getId(),"");
				break;
			}
		}

	};
	
	public void onCloseWithReturn(Object obj){
		onReturnValue(R.id.buttonShiftInfoConfirm,obj);
		dismiss();
	}

	public ShiftInfoDialog(Context context, Object base) {
		super(context,R.style.dialog,base);
	}
	
	public ShiftInfoDialog(Context context, int theme,Object base) {
		super(context, theme,base);
	}
	
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.dialog_shift_info);
		this.setCancelable(false);

		btnConfirm = (Button) findViewById(R.id.buttonShiftInfoConfirm);
		btnConfirm.setOnClickListener(mOnClickListener);

		btnClose = (Button) findViewById(R.id.buttonShiftInfoClose);
		btnClose.setOnClickListener(mOnClickListener);

		cb_tax1 = (CheckBox)findViewById(R.id.cb_tax_1);
		cb_tax2 = (CheckBox)findViewById(R.id.cb_tax_2);
		cb_tax3 = (CheckBox)findViewById(R.id.cb_tax_3);
		cb_tax4 = (CheckBox)findViewById(R.id.cb_tax_4);
		cb_tax5 = (CheckBox)findViewById(R.id.cb_tax_5);
		cb_tax6 = (CheckBox)findViewById(R.id.cb_tax_6);
		cb_foodstamp = (CheckBox)findViewById(R.id.cb_food_stamp);
		//cb_tax1.setOnClickListener(mOnClickListener);

	}
	
	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		saleList = TransactionManager.getInstance().mSaleListItems;
		
		itemIdx = saleList.mSelectIndex;
		if (itemIdx >= 0) {
			item = saleList.getItem(itemIdx);
		}
		
		if(item.getItemType() != ItemType.ENVIRONMENTAL_FEES)
		{
			itemIdx = saleList.getSelectedRegularItemIdx();
			if (itemIdx >= 0) {
				item = saleList.getItem(itemIdx);
			}
		}

		cb_tax1.setChecked(item.getTax1());
		cb_tax2.setChecked(item.getTax2());
		cb_tax3.setChecked(item.getTax3());
		cb_tax4.setChecked(item.getTax4());
		cb_tax5.setChecked(item.getTax5());
		cb_tax6.setChecked(item.getTax6());
		cb_foodstamp.setChecked(item.getFoodstamp());
		
		cb_tax1.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_tax2.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_tax3.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_tax4.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_tax5.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_tax6.setOnCheckedChangeListener(onCheckedChangeListener);
		cb_foodstamp.setOnCheckedChangeListener(onCheckedChangeListener);
		
		
	}
	
	OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {

		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

			switch (buttonView.getId()) {
			case R.id.cb_food_stamp:
				isshiftfs = true;
				break;
			default:
				isshifttax = true;
				break;
			}

		}
	};
	
}
